import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

public class CreditReport {

	public static HashMap<String, Integer> m_UserDic = new HashMap<String, Integer>(200, 0.75f); // 词典

	public static int number = 0;

	// 连接数据库
	public static final String IP = "rm-2zeeg6302tb7oqv57o.mysql.rds.aliyuncs.com";
	public static final String driver = "com.mysql.jdbc.Driver";
	public static final String url = "jdbc:mysql://" + IP
			+ ":3306/hunter2?zeroDateTimeBehavior=convertToNull&useSSL=false";
	public static final String user = "czhd";
	public static final String password = "Czhd1234";

	static Connection connect = null;
	static {
		// init("/data/credit/bin/CreditReport_new.txt");
		init("C:/myPrograms/eclipse/workspace/征信报告/征信报告词典/CreditReport_new.txt");

		// 连接数据库
		try {
			connect = DriverManager.getConnection(url, user, password);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	/**
	 * 初始化用户自定义词典
	 * C:\\Users\\Shu\\Desktop\\eclipse-jee-mars-1-win32-x86_64\\eclipse
	 * \\寻访记录\\User1.txt
	 */
	public static int init(String fileName) {
		FileReader reader = null;
		String[] str = null;
		if (fileName != null && !fileName.equals("")) {
			try {
				reader = new FileReader(new File(fileName));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			return 1;
		}
		try {
			String tempStr = "";
			BufferedReader read = new BufferedReader(reader);
			while ((tempStr = read.readLine()) != null) {
				// System.out.println(tempStr);
				str = tempStr.split(",|/");
				m_UserDic.put(str[0], Integer.parseInt(str[1]));
			}
			str = null;
			tempStr = null;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
					reader = null;
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		// Connection connect = null;
		/*
		 * try { connect = DriverManager.getConnection(
		 * "jdbc:mysql://123.57.35.10:3306/hunter2","root","hunter"); } catch
		 * (SQLException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
		// credit_report_computation();

		CreditReport cr = new CreditReport();
		cr.test();
		// System.out.println(school_score("中国人民解放军炮兵指挥学院"));
		// System.out.println("number = " + number);
		/*
		 * try { connect.close(); } catch (SQLException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
		/*
		 * FileInputStream fis = null; InputStreamReader isr = null;
		 * BufferedReader br = null; String line = ""; fis = new
		 * FileInputStream(new
		 * File("C:\\Users\\Shu\\Desktop\\test_credit.txt")); isr = new
		 * InputStreamReader(fis, "utf8"); br = new BufferedReader(isr); while
		 * ((line = br.readLine()) != null) { for(int i = 0;i <= 6;i++) {
		 * test(line,i); } }
		 */
	}

	static void credit_report_computation() {
		int i = 1;
		while (true) {
			// System.out.println(i);
			int candidateid = 0;

			// String sql = "select * from cv where credit_flag = 0";
			// System.out.println(System.currentTimeMillis());
			String sql = "select * from cv where id > " + 1000 * (i - 1) + " and id <= " + 1000 * i + " and flag=1";// test
			// System.out.println(System.currentTimeMillis());
			try {
				Statement stmt = connect.createStatement();
				ResultSet rs = stmt.executeQuery(sql);

				while (rs.next()) {
					candidateid = rs.getInt(1);
					// System.out.println();
					// System.out.println(System.currentTimeMillis());
					computation(candidateid);// 计算分数
					// System.out.println(System.currentTimeMillis());
				}
				connect.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			i++;
		}
	}

	@Test
	public void test() {

		computation(55796415);
	}

	static void computation(int candidateid) {
		// System.out.println("candidateid = " + candidateid);
		String basic_school = null;
		String basic_degree = "0";
		String basic_major = null;
		String basic_company = null;
		String basic_title = null;
		String skill_separate = "";
		String id_card = "";
		String execute_ids = "";
		String shixin_ids = "";
		
		double score_education = 300.0;// start line 300 教育经历
		double score_job = 400.0; // 工作经历
		double score_skill = 400.0; // 个人技能
		double score_identity = 300.0;// start line 300 身份特质 total
		double score_relationship = 400.0;// start line 300 //人脉关系
		double score_history = 400.0;// start line 300 // 信用历史

		int job_time = 0;

		double score_school = 0.6;
		double score_degree = 0.0;

		String max_school = null;
		int max_degree = 1;

		// Connection connect = null;
		// connect = DriverManager.getConnection(
		// "jdbc:mysql://123.57.35.10:3306/hunter2","root","hunter");
		String sql = "select * from cv_basic where candidateid=" + candidateid;
		try {
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sql);

			if (rs.next()) { // 57

				int m1 = rs.getInt(1);// 或者为rs.getString(1)，根据数据库中列的值类型确定，参数为第一列
				// System.out.println("Id = " + m1);
				int m2 = rs.getInt(2);
				// System.out.println("candidateId = " + m2);
				String m6 = rs.getString(6); // name
				// System.out.println("name = " + m6);
				id_card = rs.getString(11); // id_card
				String m12 = rs.getString(12); // company
				// if(m12 != null && !m12.equals(""))
				basic_company = m12;
				// System.out.println("company = " + m12);
				String m13 = rs.getString(13); // title
				// if(m13 != null && !m13.equals(""))
				basic_title = m13;
				// System.out.println("title = " + m13);
				String m22 = rs.getString(22); // major
				// if(m22 != null && !m22.equals(""))
				basic_major = m22;
				// System.out.println("major = " + m22);
				String m23 = rs.getString(23); // degree
				if (m23 != null && !m23.equals(""))
					basic_degree = m23;
				// System.out.println("degree = " + m23);
				String m24 = rs.getString(24); // school
				// if(m24 != null && !m24.equals(""))
				basic_school = m24;
				// System.out.println("school = " + m24);
				String m56 = rs.getString(56); // name
				// System.out.println("jobtime = " + m56);
				if (m56 != null) {
					job_time = Integer.parseInt(m56);
				}
				/* 学校评估 */
				if (m_UserDic.containsKey(m24)) {
					int type = m_UserDic.get(m24);
					switch (type) {
					case 100:
						// System.out.println("985以上");
						break;
					case 9851:
						// System.out.println("985以上");
						break;
					case 9852:
						// System.out.println("985二批");
						break;
					case 211:
						// System.out.println("211");
						break;
					case 1:
						// System.out.println("普通一本");
						break;
					case 2:
						// System.out.println("普通二本");
						break;
					case 3:
						// System.out.println("三本");
						break;
					default:
						// System.out.println("专科及以下");
						break;
					}// switch
				} // if
				else {
					// System.out.println("专科及以下");
				}
			}
			// 可以将查找到的值写入类，然后返回相应的对象
		} catch (Exception e) {
			e.printStackTrace();
		}

		// cv_education
		sql = "select * from cv_education where candidateid=" + candidateid;
		try {
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sql);

			while (rs.next()) {
				String m8 = rs.getString(8); // school
				// System.out.println("school = " + m8);
				String m9 = rs.getString(9); // major
				// System.out.println("major = " + m9);
				int m10 = rs.getInt(10); // degree
				// System.out.println("degree = " + m10);

				// use dictionary
				/*
				 * if (m_UserDic.containsKey(m8)) { int type =
				 * m_UserDic.get(m8); if(type >= 100001 && type <=
				 * 100099)//世界100 { if(m10 >= max_degree) { max_school = m8;
				 * max_degree = m10; score_school = 1 - (type - 100001)*0.001; }
				 * } else if(type >= 985101 && type <= 985109)//985一批 { if(m10
				 * >= max_degree) { max_school = m8; max_degree = m10;
				 * score_school = 1 - (type - 985101)*0.01; } } else if(type >=
				 * 985201 && type <= 985229)//985二批 { if(m10 >= max_degree) {
				 * max_school = m8; max_degree = m10; score_school = 0.9 - (type
				 * - 985201)*(0.1/29); } } else if(type >= 21101 && type <=
				 * 21173)//211 { if(m10 >= max_degree) { max_school = m8;
				 * max_degree = m10; score_school = 0.8 - (type -
				 * 21101)*(0.1/173.0); } } else if(type >= 1001 && type <=
				 * 1248)//普通一本 { if(m10 >= max_degree) { max_school = m8;
				 * max_degree = m10; score_school = 0.7 - (type -
				 * 1001)*(0.1/248.0); } } else if(type >= 2001 && type <=
				 * 2365)//普通二本 { if(m10 >= max_degree) { max_school = m8;
				 * max_degree = m10; score_school = 0.6 - (type -
				 * 2001)*(0.1/365.0); } } else if(type >= 3001 && type <=
				 * 3406)//三本 { if(m10 >= max_degree) { max_school = m8;
				 * max_degree = m10; score_school = 0.5 - (type -
				 * 3001)*(0.1/406.0); } } else { if(m10 >= max_degree) {
				 * max_school = m8; max_degree = m10; score_school = 0.4; } }
				 * }// if else { if(m10 >= max_degree) { max_school = m8;
				 * max_degree = m10; score_school = 0.4; } }
				 */

				// use database
				if (m10 >= max_degree) {
					max_degree = m10;
					score_school = school_score(m8);
				}
				// System.out.println("score_school = " + score_school);

			}
			/*
			 * System.out.println("max_degree = " + max_degree);
			 * System.out.println("max_school = " + max_school);
			 */
			score_degree = (max_degree - 1) * 100;
			/*
			 * System.out.println("max_degree = " + max_degree);
			 * System.out.println("score_degree = " + score_degree);
			 * System.out.println("max_school = " + max_school);
			 * System.out.println("score_school = " + score_school);
			 */
			// System.out.println("score_degree = " + score_degree);
			score_education = score_degree + 500 * score_school;
			// System.out.println("score_degree = " + score_degree);
			// System.out.println("score_school = " + score_school);
			if (score_education == 0)// 没有教育经历，从基本信息中提取
			{
				if (Integer.parseInt(basic_degree) != 0) {
					score_degree = (Integer.parseInt(basic_degree) - 1) * 100;
				} else {
					score_degree = 0;
				}
				if (basic_school != null && !basic_school.equals("")) {

					// use dictionary
					/*	
					 * if (m_UserDic.containsKey(basic_school)) { int type =
					 * m_UserDic.get(basic_school); if(type >= 100001 && type <=
					 * 100099)//世界100 { score_school = 1 - (type -
					 * 100001)*0.001; } else if(type >= 985101 && type <=
					 * 985109)//985一批 { score_school = 1 - (type - 985101)*0.01;
					 * } else if(type >= 985201 && type <= 985229)//985二批 {
					 * score_school = 0.9 - (type - 985201)*(0.1/29); } else
					 * if(type >= 21101 && type <= 21173)//211 { score_school =
					 * 0.8 - (type - 21101)*(0.1/173.0); } else if(type >= 1001
					 * && type <= 1248)//普通一本 { score_school = 0.7 - (type -
					 * 1001)*(0.1/248.0); } else if(type >= 2001 && type <=
					 * 2365)//普通二本 { score_school = 0.6 - (type -
					 * 2001)*(0.1/365.0); } else if(type >= 3001 && type <=
					 * 3406)//三本 { score_school = 0.5 - (type -
					 * 3001)*(0.1/406.0); } else { score_school = 0.4; } } else
					 * { score_school = 0.4; }
					 */

					// use database
					score_school = school_score(basic_school);
					// System.out.println("score_school = " + score_school);

				}
				// System.out.println("score_degree = " + score_degree);
				score_education = score_degree + 500 * score_school;
				// System.out.println("score_degree = " + score_degree);
				// System.out.println("score_school = " + score_school);
			}
			// System.out.println("score_education = " + score_education);
			if (score_education > 1000) {
				score_education = 1000;
			}
			if (score_education < 300) {
				score_education = 300;
			}
			// System.out.println("score_education = " + score_education);
			// 可以将查找到的值写入类，然后返回相应的对象
		} catch (Exception e) {
			e.printStackTrace();
		}

		// cv_job
		sql = "select * from cv_job where candidateid=" + candidateid;
		try {
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sql);

			while (rs.next()) {
				double score_job_time = 0;
				double score_company = 0;
				int m6 = rs.getInt(6);// 开始时间
				
				
				// System.out.println("startTime = " + m6);
				
				String tmp = rs.getString(7);
				int m7 = 0;
				if(tmp!=null){
					Pattern p = Pattern.compile("[0-9]{4}");
					Matcher m = p.matcher(tmp);
					
					if (m.find()) {
						m7 = Integer.parseInt(m.group());
					} else {
						if (tmp.indexOf("至今") != -1) {
							m7 = 2016;
						}
					}
				}
				
				
				//int m7 = rs.getInt(7);// 结束时间
				 System.out.println("endTime = " + m7);
				String m8 = rs.getString(8); // 公司
				// System.out.println("company = " + m8);
				String m9 = rs.getString(9); // 部门
				// System.out.println("department = " + m9);
				String m10 = rs.getString(10); // 职位
				// System.out.println("title = " + m10);
				if (m_UserDic.containsKey(m8)) {
					int type = m_UserDic.get(m8);
					if (type >= 5001001 && type <= 5001501)// 世界500强
					{
						score_company = 500 - (type - 5001001) * (50.0 / 501);
						if (m7 - m6 < 1) {
							score_job_time = 0.6;
						} else if ((m7 - m6 >= 1) && (m7 - m6 < 2)) {
							score_job_time = 0.7;
						} else if ((m7 - m6 >= 2) && (m7 - m6 < 3)) {
							score_job_time = 0.8;
						} else if ((m7 - m6 >= 3) && (m7 - m6 < 4)) {
							score_job_time = 1;
						} else if ((m7 - m6 >= 4) && (m7 - m6 < 5)) {
							score_job_time = 0.9;
						} else if ((m7 - m6 >= 5)) {
							score_job_time = 0.8;
						}
						score_job += score_company * score_job_time * 0.2;
					} else if (type >= 5002001 && type <= 5002457)// 国内500强
					{
						score_company = 450 - (type - 5002001) * (50.0 / 457);
						if (m7 - m6 < 1) {
							score_job_time = 0.6;
						} else if ((m7 - m6 >= 1) && (m7 - m6 < 2)) {
							score_job_time = 0.7;
						} else if ((m7 - m6 >= 2) && (m7 - m6 < 3)) {
							score_job_time = 0.8;
						} else if ((m7 - m6 >= 3) && (m7 - m6 < 4)) {
							score_job_time = 1;
						} else if ((m7 - m6 >= 4) && (m7 - m6 < 5)) {
							score_job_time = 0.9;
						} else if ((m7 - m6 >= 5)) {
							score_job_time = 0.8;
						}
						score_job += score_company * score_job_time * 0.2;
					} else// 其他公司
					{
						score_company = 400.0 - Math.random() * 10.0;
						if (m7 - m6 < 1) {
							score_job_time = 0.6;
						} else if ((m7 - m6 >= 1) && (m7 - m6 < 2)) {
							score_job_time = 0.7;
						} else if ((m7 - m6 >= 2) && (m7 - m6 < 3)) {
							score_job_time = 0.8;
						} else if ((m7 - m6 >= 3) && (m7 - m6 < 4)) {
							score_job_time = 1;
						} else if ((m7 - m6 >= 4) && (m7 - m6 < 5)) {
							score_job_time = 0.9;
						} else if ((m7 - m6 >= 5)) {
							score_job_time = 0.8;
						}
						score_job += score_company * score_job_time * 0.2;
					}
				} // if
				else {
					score_company = 400.0 - Math.random() * 10.0;
					if (m7 - m6 < 1) {
						score_job_time = 0.6;
					} else if ((m7 - m6 >= 1) && (m7 - m6 < 2)) {
						score_job_time = 0.7;
					} else if ((m7 - m6 >= 2) && (m7 - m6 < 3)) {
						score_job_time = 0.8;
					} else if ((m7 - m6 >= 3) && (m7 - m6 < 4)) {
						score_job_time = 1;
					} else if ((m7 - m6 >= 4) && (m7 - m6 < 5)) {
						score_job_time = 0.9;
					} else if ((m7 - m6 >= 5)) {
						score_job_time = 0.8;
					}
					score_job += score_company * score_job_time * 0.2;
				}
				/*
				 * System.out.println("company = " + m8); System.out.println(
				 * "score_company = " + score_company); System.out.println(
				 * "job_time = " + (m7 - m6)); System.out.println(
				 * "score_job_time = " + score_job_time);
				 */
				// System.out.println("score_job = " + score_job);
			}
			if (score_job > 1000) {
				score_job = 1000;
			}

			if (score_job == 400)// 没有工作经历，从基本信息中提取
			{
				double score_company = 0;
				double score_job_time = 0.6;
				if (m_UserDic.containsKey(basic_company)) {
					int type = m_UserDic.get(basic_company);
					if (type >= 5001001 && type <= 5001501) {
						score_company = 500 - (type - 5001001) * (50.0 / 501);
						score_job += score_company * score_job_time * 0.2;
					} else if (type >= 5002001 && type <= 5002457) {
						score_company = 450 - (type - 5002001) * (50.0 / 457);
						score_job += score_company * score_job_time * 0.2;
					} else {
						score_company = 400.0 - Math.random() * 10.0;
						score_job += score_company * score_job_time * 0.2;
					}
				} // if
				else {
					score_company = 400.0 - Math.random() * 10.0;
					score_job += score_company * score_job_time * 0.2;
				}
			}
			if (score_job < 300) {
				score_job = 300;
			}
			// System.out.println("score_job = " + score_job);
			// 可以将查找到的值写入类，然后返回相应的对象
		} catch (Exception e) {
			e.printStackTrace();
		}

		// cv_skill
		sql = "select * from cv_skill where candidateid=" + candidateid;
		try {
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			int num = 0;
			while (rs.next()) {
				double experience = 0;
				double skill_info = 0;
				String m1 = rs.getString(1);
				String m6 = rs.getString(6);
				// System.out.println("skill_name = " + m6);
				String m9 = rs.getString(9);
				// System.out.println("skill_info = " + m9);
				int m11 = rs.getInt(11);
				// System.out.println("experience = " + m11);
				if (m9 != null) {
					if (m9.equals("了解")) {
						skill_info = 80 + Math.random();
					} else if (m9.equals("一般") || m9.equals("熟悉") || m9.equals("熟练")) {
						skill_info = 120 + Math.random();
					} else if (m9.equals("良好")) {
						skill_info = 160 + Math.random();
					} else if (m9.equals("精通")) {
						skill_info = 200 + Math.random();
					} else {
						skill_info = 20 + Math.random();
					}
				}

				if (m11 < 6) {
					experience = 0.6 - (5 - m11) * (0.1 / 6);
				} else if ((m11 >= 6) && (m11 < 12)) {
					experience = 0.7 - (11 - m11) * (0.1 / 6);
				} else if ((m11 >= 12) && (m11 < 18)) {
					experience = 0.8 - (17 - m11) * (0.1 / 6);
				} else if ((m11 >= 18) && (m11 < 24)) {
					experience = 0.9 - (23 - m11) * (0.1 / 6);
				} else if ((m11 >= 24)) {
					experience = 0.9 + (m11 - 24) * (0.002);
				} else {
					experience = 0.4;
				}
				score_skill += skill_info * experience;
				if ((skill_info * experience * 3.5 + 400) > 1000) {
					skill_separate += m1 + "," + (1000 * 0.2 + score_education * 0.4 + score_job * 0.4) + ";";
				} else {
					skill_separate += m1 + ","
							+ ((skill_info * experience * 3.5 + 400) * 0.2 + score_education * 0.4 + score_job * 0.4)
							+ ";";
				}
				// skill_separate += m6 + "," + skill_info*experience*3.5 + 30 +
				// ";";
				/*
				 * System.out.println("skill_info = " + skill_info);
				 * System.out.println("experience = " + experience);
				 * System.out.println("score_skill = " + score_skill);
				 */
				num++;
			}
			// System.out.println("score_skill = " + score_skill);
			// score_skill = score_skill / num;
			// System.out.println("score_skill = " + score_skill);
			if (score_skill > 1000) {
				score_skill = 1000;
			}
			if (score_skill <= 0) {

			} else {
				score_skill = score_education * 0.4 + score_job * 0.4 + score_skill * 0.2;
			}
			if (score_skill < 300) {
				score_skill = 300;
			}
			// System.out.println("score_skill = " + score_skill);
			// 可以将查找到的值写入类，然后返回相应的对象
		} catch (Exception e) {
			e.printStackTrace();
		}

		// shixin
		if (id_card != null && !id_card.equals("") && !id_card.equals("保密")) {
			// System.out.println("execute");
			sql = "select * from shixin where partyCardNum='" + id_card + "'";
			try {
				Statement stmt = connect.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				int num = 0;
				while (rs.next()) {
					if (execute_ids.length() < 1000) {
						execute_ids += rs.getInt(1) + ";";
					}
					num++;
				}
				score_history = score_history - num * 10;
				if (score_history > 1000) {
					score_history = 1000;
				}
				if (score_history <= 0) {
					score_history = 0;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		sql = "select * from cv_credit_record where cv_id=" + candidateid;
		try {
			// System.out.println("shixin");
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			int num = 0;
			while (rs.next()) {
				shixin_ids += rs.getInt(1) + ";";
				num++;
			}
			score_history = score_history - num * 5;
			if (score_history > 1000) {
				score_history = 1000;
			}
			if (score_history <= 0) {
				score_history = 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 征信分转换
		double score_history_new = score_history * 0.2 + score_education * 0.3 + score_job * 0.5;
		double score_relationship_new = score_relationship * 0.2 + score_education * 0.4 + score_job * 0.4;
		
		// 打印份数

		// 征信分入库
		sql = "select * from Credit_Report where candidateid=" + candidateid;
		try {
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sql);

			if (rs.next()) {
				// update
				// update(candidateid,score_education,score_skill,score_job,score_history,score_relationship,job_time,skill_separate,execute_ids,shixin_ids);
				update(candidateid, score_education, score_skill, score_job, score_history_new, score_relationship_new,
						job_time, skill_separate, execute_ids, shixin_ids);
			} else {
				// create
				// create(candidateid,score_education,score_skill,score_job,score_history,score_relationship,job_time,skill_separate,execute_ids,shixin_ids);
				create(candidateid, score_education, score_skill, score_job, score_history_new, score_relationship_new,
						job_time, skill_separate, execute_ids, shixin_ids);
			}
			// 可以将查找到的值写入类，然后返回相应的对象
		} catch (Exception e) {
			e.printStackTrace();
		}
		number++;
		// System.out.println("number = " + number);
	}

	// 用户不存在，新建
	static void create(int candidateid, double education_score, double skill_score, double job_score,
			double history_score, double relationship_score, int job_time, String skill_separate, String execute_ids,
			String shixin_ids) {
		// System.out.println("create");
		// connect = DriverManager.getConnection(
		// "jdbc:mysql://123.57.35.10:3306/hunter2","root","hunter");
		try {
			double score_education = (education_score);
			double score_skill = (skill_score);
			double score_job = (job_score);
			double score_history = (history_score);
			double score_relationship = (relationship_score);
			int shixin_param = 100; // 100 -> 36.8
			int skill_param = 100;
			int relationship_param = 100;

			double job_rate = 0.5;
			double education_rate = 0.5;
			if (job_time > 5) {
				job_time = 5;
			} else if (job_time < 1) {
				job_time = 1;
			}
			job_rate = 1 - (5 - job_time) * 0.1 - 0.3;
			education_rate = (5 - job_time) * 0.1 + 0.3;
			double total_score = (300 + (score_education * education_rate + score_job * job_rate) * 0.6
					+ skill_param * (skill_score / 1000.0) * Math.pow(Math.E, -(skill_score / 1000.0))
					+ relationship_param * (relationship_score / 1000.0)
							* Math.pow(Math.E, -(relationship_score / 1000.0))
					- shixin_param * ((1000 - history_score) / 1000.0)
							* Math.pow(Math.E, -((1000 - history_score) / 1000.0)));

			Date date = new Date();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = format.format(date);
			PreparedStatement stmt;
			stmt = connect.prepareStatement("INSERT INTO Credit_Report VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
					PreparedStatement.RETURN_GENERATED_KEYS);
			stmt.setString(1, null);
			stmt.setInt(2, candidateid);
			// int total_score = (int) (50 + score_education*0.1 +
			// score_skill*0.1 + score_job*0.1 + score_history*0.1 +
			// score_relationship*0.1);
			if (total_score > 1000)
				total_score = 1000;
			stmt.setString(3, total_score + "");// total score
			stmt.setString(4, total_score + "");// identity score
			stmt.setString(5, score_relationship + "");// relationship score
			stmt.setString(6, score_history + "");// credit history
			stmt.setString(7, score_education + "");// education score
			stmt.setString(8, score_skill + "");// skill score
			stmt.setString(9, score_job + "");// job score
			stmt.setInt(10, 1);
			stmt.setString(11, time);
			stmt.setString(12, time);
			stmt.setString(13, skill_separate);
			stmt.setString(14, execute_ids);
			stmt.setString(15, shixin_ids);
			// stmt.executeUpdate();

			// 更新cv表flag
			// stmt=connect.prepareStatement("update Credit_Report set
			// credit_flag=1 where id=" +
			// candidateid,PreparedStatement.RETURN_GENERATED_KEYS);
			// stmt.executeUpdate();

			// 打印征信单
		 
			
			String line = candidateid + "\t" + total_score + "\t" + score_relationship + "\t" + score_history + "\t"
					+ score_education + "\t" + score_education + "\t" + score_skill + "\t" + score_job + "\t"
					+ skill_separate + "\t" + execute_ids + "\t" + shixin_ids;
			System.out.println("征信分：" + line);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 用户已存在，更新
	static void update(int candidateid, double education_score, double skill_score, double job_score,
			double history_score, double relationship_score, int job_time, String skill_separate, String execute_ids,
			String shixin_ids) {
		// connect = DriverManager.getConnection(
		// "jdbc:mysql://123.57.35.10:3306/hunter2","root","hunter");
		try {
			double score_education = (education_score);
			double score_skill = (skill_score);
			double score_job = (job_score);
			double score_history = (history_score);
			double score_relationship = (relationship_score);
			int shixin_param = 100;// 100 -> 36.8, 10 -> 3.68, 1 -> 0.368
			int skill_param = 100;
			int relationship_param = 100;

			double job_rate = 0.5;
			double education_rate = 0.5;
			if (job_time > 5) {
				job_time = 5;
			} else if (job_time < 1) {
				job_time = 1;
			}
			job_rate = 1 - (5 - job_time) * 0.1 - 0.3;
			education_rate = (5 - job_time) * 0.1 + 0.3;
			double total_score = (300 + (score_education * education_rate + score_job * job_rate) * 0.6
					+ skill_param * (skill_score / 1000.0) * Math.pow(Math.E, -(skill_score / 1000.0))
					+ relationship_param * (relationship_score / 1000.0)
							* Math.pow(Math.E, -(relationship_score / 1000.0))
					- shixin_param * ((1000 - history_score) / 1000.0)
							* Math.pow(Math.E, -((1000 - history_score) / 1000.0)));

			Date date = new Date();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = format.format(date);
			PreparedStatement stmt;
			stmt = connect.prepareStatement(
					"update Credit_Report set total_score=?,identity_score=?,relationship_score=?,credit_history=?,education_score=?,skill_score=?,job_score=?,update_time=?,skill_separate=?,execute_ids=?,shixin_ids=? where candidateid="
							+ candidateid,
					PreparedStatement.RETURN_GENERATED_KEYS);
			// int total_score = (int) (50 + score_education*0.1 +
			// score_skill*0.1 + score_job*0.1 + score_history*0.1 +
			// score_relationship*0.1);
			if (total_score > 1000)
				total_score = 1000;
			stmt.setString(1, total_score + ""); // total
			stmt.setString(2, total_score + ""); // identity
			stmt.setString(3, score_relationship + "");// relationship score
			stmt.setString(4, score_history + "");// credit history
			stmt.setString(5, score_education + "");// education score
			stmt.setString(6, score_skill + "");// skill score
			stmt.setString(7, score_job + "");// job score
			stmt.setString(8, time);// update time
			stmt.setString(9, skill_separate);
			stmt.setString(10, execute_ids);
			stmt.setString(11, shixin_ids);
			// stmt.executeUpdate();
			
			// 更新cv表flag
			// stmt=connect.prepareStatement("update Credit_Report set
			// credit_flag=1 where id=" +
			// candidateid,PreparedStatement.RETURN_GENERATED_KEYS);
			// stmt.executeUpdate();

			String line = candidateid + "\t" + total_score + "\t" + score_relationship + "\t" + score_history + "\t"
					+ score_education + "\t" + score_education + "\t" + score_skill + "\t" + score_job + "\t"
					+ skill_separate + "\t" + execute_ids + "\t" + shixin_ids;
			System.out.println("征信分：" + line);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static double school_score(String name) {
		double school_score = 0.6 + Math.random() * 0.02 - 0.01;
		String sql = "select * from university_rank where name='" + name + "'";
		try {
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				school_score = rs.getDouble(5);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return school_score;
	}
	
	/*
	 * static void test(String company,Integer jobtime) { //String school = "";
	 * //int degree = 0; System.out.println("school = " + school);
	 * System.out.println("degree = " + degree); double score_school = 0.0;
	 * double score_degree = 0.0; double score_education = 0.0;
	 * 
	 * if (m_UserDic.containsKey(school)) { int type = m_UserDic.get(school);
	 * if(type >= 100001 && type <= 100099)//世界100 { score_school = 1 - (type -
	 * 100001)*0.001; System.out.println("世界100"); } else if(type >= 985101 &&
	 * type <= 985109)//985一批 { score_school = 1 - (type - 985101)*0.01;
	 * System.out.println("985一批"); } else if(type >= 985201 && type <=
	 * 985229)//985二批 { score_school = 0.9 - (type - 985201)*(0.1/29);
	 * System.out.println("985二批"); } else if(type >= 21101 && type <=
	 * 21173)//211 { score_school = 0.8 - (type - 21101)*(0.1/173.0);
	 * System.out.println("211"); } else if(type >= 1001 && type <= 1248)//普通一本
	 * { score_school = 0.7 - (type - 1001)*(0.1/248.0);
	 * System.out.println("普通一本"); } else if(type >= 2001 && type <= 2365)//普通二本
	 * { score_school = 0.6 - (type - 2001)*(0.1/365.0);
	 * System.out.println("普通二本"); } else if(type >= 3001 && type <= 3406)//三本 {
	 * score_school = 0.5 - (type - 3001)*(0.1/406.0); System.out.println("三本");
	 * } else { score_school = 0.4; System.out.println("其它"); } }// if else {
	 * score_school = 0.4; //System.out.println("其它"); } score_degree = (degree
	 * - 1) * 10; score_education = score_degree + 50 * score_school;
	 * System.out.println("score_education = " + score_education);
	 * System.out.println("");
	 * 
	 * 
	 * 
	 * double score_job_time = 0; double score_company = 0; double score_job =
	 * 0.0; System.out.println("company = " + company); System.out.println(
	 * "jobtime = " + jobtime); if (m_UserDic.containsKey(company)) { int type =
	 * m_UserDic.get(company); if(type >= 5001001 && type <= 5001501)//世界500强 {
	 * System.out.println("世界500强"); score_company = 50 - (type -
	 * 5001001)*(10.0/501); if(jobtime < 1) { score_job_time = 0.2; } else
	 * if((jobtime >= 1) && (jobtime < 2)) { score_job_time = 0.4; } else
	 * if((jobtime >= 2) && (jobtime < 3)) { score_job_time = 0.8; } else
	 * if((jobtime >= 3) && (jobtime < 4)) { score_job_time = 1; } else
	 * if((jobtime >= 4) && (jobtime < 5)) { score_job_time = 0.9; } else
	 * if((jobtime >= 5)) { score_job_time = 0.8; } score_job += score_company *
	 * score_job_time; } else if(type >= 5002001 && type <= 5002457)//国内500强 {
	 * System.out.println("国内500强"); score_company = 40 - (type -
	 * 5002001)*(10.0/457); if(jobtime < 1) { score_job_time = 0.2; } else
	 * if((jobtime >= 1) && (jobtime < 2)) { score_job_time = 0.4; } else
	 * if((jobtime >= 2) && (jobtime < 3)) { score_job_time = 0.8; } else
	 * if((jobtime >= 3) && (jobtime < 4)) { score_job_time = 1; } else
	 * if((jobtime >= 4) && (jobtime < 5)) { score_job_time = 0.9; } else
	 * if((jobtime >= 5)) { score_job_time = 0.8; } score_job += score_company *
	 * score_job_time; } else//其他公司 { System.out.println("其他公司"); score_company
	 * = 20; if(jobtime < 1) { score_job_time = 0.2; } else if((jobtime >= 1) &&
	 * (jobtime < 2)) { score_job_time = 0.4; } else if((jobtime >= 2) &&
	 * (jobtime < 3)) { score_job_time = 0.8; } else if((jobtime >= 3) &&
	 * (jobtime < 4)) { score_job_time = 1; } else if((jobtime >= 4) && (jobtime
	 * < 5)) { score_job_time = 0.9; } else if((jobtime >= 5)) { score_job_time
	 * = 0.8; } score_job += score_company * score_job_time; } }// if else {
	 * System.out.println("其他公司"); score_company = 20; if(jobtime < 1) {
	 * score_job_time = 0.2; } else if((jobtime >= 1) && (jobtime < 2)) {
	 * score_job_time = 0.4; } else if((jobtime >= 2) && (jobtime < 3)) {
	 * score_job_time = 0.8; } else if((jobtime >= 3) && (jobtime < 4)) {
	 * score_job_time = 1; } else if((jobtime >= 4) && (jobtime < 5)) {
	 * score_job_time = 0.9; } else if((jobtime >= 5)) { score_job_time = 0.8; }
	 * score_job += score_company * score_job_time; } System.out.println(
	 * "company = " + m8); System.out.println("score_company = " +
	 * score_company); System.out.println("job_time = " + (m7 - m6));
	 * System.out.println("score_job_time = " + score_job_time);
	 * System.out.println("score_job = " + score_job); System.out.println(""); }
	 */
}
